/**
 * @File Name: Log.h
 * @brief  
 * @Author : LiWanglin email:lwl510ll@163.com
 * @Version : 1.0
 * @Creat Date : 2023-12-03
 * 
 * @copyright Copyright (c) 2023 LiWanglin
 */

#ifndef _LOG_LOG_H
#define _LOG_LOG_H

#include "process_manager.h"

#define LW_SET_LOG_PATH                 0x02010101
#define LW_SET_ENABLE_FILE_WRITE        0x02010103

enum LogLevel {
    LOG_LEVEL_DEBUG = 0,
    LOG_LEVEL_INFO,
    LOG_LEVEL_WARN,
    LOG_LEVEL_ERROR,
    LOG_LEVEL_FATAL,
};

#define lw_debug(...) { \
    do { \
        Log::get_instance()->out(LOG_LEVEL_DEBUG, __FILE__, __func__, __LINE__, __VA_ARGS__); \
    }while(0);  \
}

#define lw_info(...) { \
    do { \
        Log::get_instance()->out(LOG_LEVEL_INFO, __FILE__, __func__, __LINE__, __VA_ARGS__); \
    }while(0);  \
}

#define lw_warn(...) { \
    do { \
        Log::get_instance()->out(LOG_LEVEL_WARN, __FILE__, __func__, __LINE__, __VA_ARGS__); \
    }while(0);  \
}

#define lw_error(...) { \
    do { \
        Log::get_instance()->out(LOG_LEVEL_ERROR, __FILE__, __func__, __LINE__, __VA_ARGS__); \
    }while(0);  \
}

#define lw_fatal(...) { \
    do { \
        Log::get_instance()->out(LOG_LEVEL_FATAL, __FILE__, __func__, __LINE__, __VA_ARGS__); \
    }while(0);  \
}

class Log: public ProcessManager
{
private:
    const char *save_path;
    int32_t file_size;
    int32_t back_count;
    int32_t log_fd;
    bool start_flag;
    bool print_flag;
    bool write_file_flag;
public:
    static Log *get_instance();
    Log();
    Log(const char *path, int32_t size=1024, int32_t count=1);
    ~Log();


    virtual CMResult set(int type, void *arg) override;
    virtual CMResult init() override;
    virtual CMResult uninit() override;
    virtual CMResult start() override;
    virtual CMResult stop() override;
    CMResult out(LogLevel level, const char *file, const char *func, int line, const char *format, ...);
};

#endif /* _LOG_LOG_H */